package net.caixiaomi.info.ui.discovery;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.LeagueHotAdapter;
import net.caixiaomi.info.adapter.DiscoveryScorerListAdapter;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.DiscoveryHallEntity;
import net.caixiaomi.info.model.DiscoveryHomePageEntity;
import net.caixiaomi.info.model.DiscoveryScorerEntity;
import net.caixiaomi.info.model.LeagueGroupEntity;
import net.caixiaomi.info.model.LeagueInfoEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.SchemeUtil;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by mac on 2018/9/7.
 */

public class LeagueHotFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener {

    private LeagueHotAdapter mGridViewAdapter0;

    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;

    @BindView(android.R.id.list)
    RecyclerView mListView;

    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private int mGroupId;
    private boolean firstLoad=true;

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        loadData();
    }

    @Override
    protected int fragmentLayout() {
        return R.layout.frag_common_list;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {

        mListView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mGridViewAdapter0 = new LeagueHotAdapter(R.layout.discovery_main_select_item);
        mListView.setAdapter(mGridViewAdapter0);
        mRefresh.setOnRefreshListener(this);
        mRefresh.setBackgroundResource(R.color.white);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    public void initData(int id){
        mGroupId=id;
        if(firstLoad){
            firstLoad=false;
            loadData();
        }
    }

    public void setGroupId(int id){
        mGroupId=id;
        if(firstLoad){
            firstLoad=false;
            loadData();
        }
    }

    private void loadData() {
        JSONObject params = new JSONObject();
        params.put("groupId", mGroupId);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<LeagueGroupEntity>> dis = requestService.getDiscoveryLeagues(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<LeagueGroupEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<LeagueGroupEntity> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                bindData(data.data);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });

    }

    private void bindData(final LeagueGroupEntity entity){
        try {
            List<MultiItemEntity> list0=new ArrayList<>();
            list0.addAll(entity.getGroupLeagues());
            mGridViewAdapter0.setNewData(list0);
            mGridViewAdapter0.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    LeagueInfoEntity entity= (LeagueInfoEntity) adapter.getData().get(position);
                    Intent intent=new Intent(getActivity(),LeagueDetailActivity.class);
                    intent.putExtra("leagueId",entity.getLeagueId());
                    startActivity(intent);
                }
            });
        }catch (Exception e){

        }
    }

}
